package cn.dansj.handler.interfaces;

import cn.dansj.annotation.MarkAsApi;
import cn.dansj.bean.CurrentThreadLocalUserInfo;
import cn.dansj.common.utils.transfer.RandomUtils;
import cn.dansj.common.utils.transfer.Verification;
import cn.dansj.handler.InterfaceHandler;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@SuppressWarnings("all")
@MarkAsApi
public class TempVerificationCode extends InterfaceHandler {
    @Override
    public Object execute(Map<String, Object> paramsMap) {
        String key = "verificationCode@" + CurrentThreadLocalUserInfo.getCurrentThreadLocalUserInfo().getCacheToken().getUsername();

        String randomString = redisUtils.getString(key);
        if (Verification.checkNull(randomString)) {
            randomString = String.valueOf(RandomUtils.randomInt(10000, 99999));
            redisUtils.set(key, randomString, 1, TimeUnit.MINUTES);
        }

        long expire = redisUtils.getExpire(key);
        Map<String, Object> randomMap = new HashMap<>();
        randomMap.put("code", randomString);
        randomMap.put("expire", expire);
        return randomMap;
    }
}